using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NTTHAO.CommonWeb;
using NTTHAO.Web.BL.HighlightLink;

public partial class admin_webparts_Plug_ins_HighlightLink_GroupInput : System.Web.UI.UserControl
{
    public string _ErrorMsg = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        btnSave.Value = btnSave2.Value = LanguageUtility.GetLanguageText("SAVE", Session);
        chkActive.Text = LanguageUtility.GetLanguageText("ACTIVE", Session);
        this.txtMaxItemCount.Style[HtmlTextWriterStyle.TextAlign] = "right";
        this.txtDefaultWidth.Style[HtmlTextWriterStyle.TextAlign] = "right";
        this.txtDefaultHeight.Style[HtmlTextWriterStyle.TextAlign] = "right";
        this.txtResizeWidth.Style[HtmlTextWriterStyle.TextAlign] = "right";
        this.txtResizeHeight.Style[HtmlTextWriterStyle.TextAlign] = "right";
        this.txtThumbWidth.Style[HtmlTextWriterStyle.TextAlign] = "right";
        this.txtThumbHeight.Style[HtmlTextWriterStyle.TextAlign] = "right";

        try
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["id"] != null)
                {
                    if (!"editgroup".Equals(Request.QueryString["action"], StringComparison.OrdinalIgnoreCase))
                    {
                        throw new Exception();
                    }

                    HighlightLinkGroup group = HighlightLinkGroup.GetItem(Int32.Parse(Request.QueryString["id"]));
                    if (group.ID < 1)
                    {
                        throw new Exception();
                    }

                    this.txtName.Text = group.Name;
                    this.txtMaxItemCount.Text = group.MaxItemCount.ToString("0");
                    this.chkImageAvailable.Checked = group.ImageAvailable;
                    this.chkImageRequired.Checked = group.ImageRequired;
                    this.txtDefaultWidth.Text = group.DefaultWidth.ToString("0");
                    this.txtDefaultHeight.Text = group.DefaultHeight.ToString("0");
                    this.chkAutoResize.Checked = group.AutoResize;
                    this.txtResizeWidth.Text = group.ResizeWidth.ToString("0");
                    this.txtResizeHeight.Text = group.ResizeHeight.ToString("0");
                    this.cmbResizeOption.Value = ((short)group.ResizeOption).ToString("0");
                    this.txtResizeBackColor.Text = group.ResizeBackColor;
                    this.cmbImageQuantity.Value = group.ResizeQuantity.ToString("0");
                    this.cmbThumbImageOption.Value = ((short)group.ThumbImageOption).ToString("0");
                    this.txtThumbWidth.Text = group.ThumbImageWidth.ToString("0");
                    this.txtThumbHeight.Text = group.ThumbImageHeight.ToString("0");
                    this.cmbThumbResizeOption.Value = ((short)group.ThumbResizeOption).ToString("0");
                    this.txtThumbBackColor.Text = group.ThumbResizeBackColor;
                    this.cmbThumbQuantity.Value = group.ThumbResizeQuantity.ToString("0");
                    this.cmbDescriptionOption.Value = ((short)group.DescriptionOption).ToString("0");
                    this.txtCssUrl.Text = group.AdminDescriptionCssUrl;
                    this.chkPriceAvailable.Checked = group.PriceAvailable;
                    this.chkSaleOffAvailable.Checked = group.SaleOffAvailable;
                    this.chkUnitAvailable.Checked = group.UnitAvailable;
                    this.chkCurrencyAvailable.Checked = group.CurrencyAvailable;
                    this.txtCurrencyDefault.Text = group.CurrencyDefault;
                    this.chkActive.Checked = group.Activated;
                }
            }
        }
        catch (Exception ex)
        {
            _ErrorMsg = ex.Message + "<br /><br />" + ex.StackTrace;
            //Response.Write(ex.Message + "<br /><br />" + ex.StackTrace);
            //Response.Redirect("?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"], false);
            return;
        }
    }

    public void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            bool isEditing = "editgroup".Equals(Request.QueryString["action"], StringComparison.OrdinalIgnoreCase);

            HighlightLinkGroup group = null;
            if (isEditing)
            {
                group = HighlightLinkGroup.GetItem(Int32.Parse(Request.QueryString["id"]));
                if (group.ID < 1)
                {
                    throw new Exception("The HighlightLinkGroup does not exist.");
                }
            }
            else
            {
                group = new HighlightLinkGroup();
                group.SiteID = NTTHAO.CommonWeb.WebGlobal.GetSite().ID;
                group.LanguageID = NTTHAO.CommonWeb.LanguageUtility.GetLanguageID();
            }

            if (!chkImageAvailable.Checked)
            {
                this.chkImageRequired.Checked = group.ImageRequired;
                this.txtDefaultWidth.Text = group.DefaultWidth.ToString("0");
                this.txtDefaultHeight.Text = group.DefaultHeight.ToString("0");
                this.chkAutoResize.Checked = group.AutoResize;
                this.txtResizeWidth.Text = group.ResizeWidth.ToString("0");
                this.txtResizeHeight.Text = group.ResizeHeight.ToString("0");
                this.cmbResizeOption.Value = ((short)group.ResizeOption).ToString("0");
                this.txtResizeBackColor.Text = group.ResizeBackColor;
                this.cmbImageQuantity.Value = group.ResizeQuantity.ToString("0");

                this.cmbThumbImageOption.Value = ((short)group.ThumbImageOption).ToString("0");
                this.txtThumbWidth.Text = group.ThumbImageWidth.ToString("0");
                this.txtThumbHeight.Text = group.ThumbImageHeight.ToString("0");
                this.cmbThumbResizeOption.Value = ((short)group.ThumbResizeOption).ToString("0");
                this.txtThumbBackColor.Text = group.ThumbResizeBackColor;
                this.cmbThumbQuantity.Value = group.ThumbResizeQuantity.ToString("0");
            }
            else
            {
                if (!chkAutoResize.Checked)
                {
                    this.txtResizeWidth.Text = group.ResizeWidth.ToString("0");
                    this.txtResizeHeight.Text = group.ResizeHeight.ToString("0");
                    this.cmbResizeOption.Value = ((short)group.ResizeOption).ToString("0");
                    this.txtResizeBackColor.Text = group.ResizeBackColor;
                    this.cmbImageQuantity.Value = group.ResizeQuantity.ToString("0");
                }
                if ("0".Equals(cmbThumbImageOption.Value, StringComparison.OrdinalIgnoreCase))
                {
                    this.txtThumbWidth.Text = group.ThumbImageWidth.ToString("0");
                    this.txtThumbHeight.Text = group.ThumbImageHeight.ToString("0");
                    this.cmbThumbResizeOption.Value = ((short)group.ThumbResizeOption).ToString("0");
                    this.txtThumbBackColor.Text = group.ThumbResizeBackColor;
                    this.cmbThumbQuantity.Value = group.ThumbResizeQuantity.ToString("0");
                }
            }
            
            if (!chkPriceAvailable.Checked)
            {
                this.chkSaleOffAvailable.Checked = group.SaleOffAvailable;
                this.chkUnitAvailable.Checked = group.UnitAvailable;
                this.chkCurrencyAvailable.Checked = group.CurrencyAvailable;
                this.txtCurrencyDefault.Text = group.CurrencyDefault;
            }

            group.Name = txtName.Text.Trim();
            group.MaxItemCount = Convert.ToInt32(txtMaxItemCount.Text.Trim());
            group.ImageAvailable = chkImageAvailable.Checked;
            group.ImageRequired = chkImageRequired.Checked;
            group.DefaultWidth = Convert.ToInt32(txtDefaultWidth.Text.Trim());
            group.DefaultHeight = Convert.ToInt32(txtDefaultHeight.Text.Trim());
            group.AutoResize = chkAutoResize.Checked;
            group.ResizeWidth = Convert.ToInt32(txtResizeWidth.Text.Trim());
            group.ResizeHeight = Convert.ToInt32(txtResizeHeight.Text.Trim());
            group.ResizeOption = (NTTHAO.Imaging.ResizeOptions)Convert.ToInt16(cmbResizeOption.Value);
            group.ResizeBackColor = txtResizeBackColor.Text.Trim();
            group.ResizeQuantity = Convert.ToInt16(cmbImageQuantity.Value);

            group.ThumbImageOption = (ThumbImageOptions)Convert.ToInt16(cmbThumbImageOption.Value);
            group.ThumbImageWidth = Convert.ToInt32(txtThumbWidth.Text.Trim());
            group.ThumbImageHeight = Convert.ToInt32(txtThumbHeight.Text.Trim());
            group.ThumbResizeOption = (NTTHAO.Imaging.ResizeOptions)Convert.ToInt16(cmbThumbResizeOption.Value);
            group.ThumbResizeBackColor = txtThumbBackColor.Text.Trim();
            group.ThumbResizeQuantity = Convert.ToInt16(cmbThumbQuantity.Value);

            group.DescriptionOption = (DescriptionOptions)Convert.ToInt16(cmbDescriptionOption.Value);
            group.AdminDescriptionCssUrl = txtCssUrl.Text.Trim();
            group.PriceAvailable = chkPriceAvailable.Checked;
            group.SaleOffAvailable = chkSaleOffAvailable.Checked;
            group.UnitAvailable = chkUnitAvailable.Checked;
            group.CurrencyAvailable = chkCurrencyAvailable.Checked;
            group.CurrencyDefault = txtCurrencyDefault.Text.Trim();
            group.Activated = chkActive.Checked;

            if (isEditing)
            {
                group.Update();
            }
            else
            {
                group.Insert();
                group.ImagePath = "HLinks/" + DateTime.Now.ToString("yyyy/MM") + "/" + group.ID.ToString() + "/";
                group.Update();
            }

            string redirectURL = "?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"];
            Response.Redirect(redirectURL, false);
        }
        catch (Exception ex)
        {
            _ErrorMsg = ex.Message + "<br /><br />" + ex.StackTrace;
            return;
        }
    }
}
